﻿using System;
using System.Linq;
using System.Web.UI.WebControls;
using CustomerContactSystem.Core.Model;

namespace CustomerContactSystem.WebForms.Admin.Reps
{
    public partial class List : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack) {
                GetReps();
            }
        }

        private void GetReps() {
            var reps = RepRepository.QueryOver().List();
            RepsList.DataSource = reps.OrderBy(r => r.FullName);
            RepsList.DataBind();
        }

        protected void RepsListItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem) {
                return;
            }

            var rowRep = (Rep) e.Item.DataItem;
            var editLink = (HyperLink) e.Item.FindControl("EditLink");

            editLink.NavigateUrl = "~/Admin/Reps/Edit.aspx?id=" + rowRep.Id;
        }
    }
}